package web.cms;


import domain.cms.CmsPage;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import web.commons.CmsLink;

import java.util.List;

public final class LeftMenuPanel extends Panel {

    Long selectedCategoryId;
    Long selectedPageId;


    public LeftMenuPanel(String id, final IModel<CmsPage> sectionModel, Long selectedCategoryId, Long selectedPageId) {
        super(id);
        this.selectedCategoryId = selectedCategoryId;
        this.selectedPageId = selectedPageId;
        IModel<List<CmsPage>> categoriesModel = new AbstractReadOnlyModel<List<CmsPage>>() {

            @Override
            public List<CmsPage> getObject() {
                if (sectionModel.getObject() == null) {
                    return null;
                }
                return sectionModel.getObject().getChilds();
            }
        };
        CmsCategoryListView categoryListView = new CmsCategoryListView("list", categoriesModel);
        add(categoryListView);
        add(new CmsLink("sectionLink", sectionModel));
    }

    public LeftMenuPanel(String id, IModel<CmsPage> sectionModel) {
        this(id, sectionModel, null, null);

    }
//    public  LeftMenuPanel(String id, IModel<CmsPage> sectionModel, Long categoryId) {
//        super(id);
//    }

    class CmsCategoryListView extends ListView<CmsPage> {

        public CmsCategoryListView(String id, IModel<? extends List<? extends CmsPage>> model) {
            super(id, model);
        }

        @Override
        protected void populateItem(final ListItem<CmsPage> item) {
            WebMarkupContainer container = new WebMarkupContainer("categoryContainer");
            item.add(container);
            CmsLink link = new CmsLink("link", item.getModelObject());
            container.add(link);

            if (selectedCategoryId != null && selectedCategoryId.equals(item.getModelObject().getId())) {
                IModel<List<CmsPage>> model = new AbstractReadOnlyModel<List<CmsPage>>() {

                    @Override
                    public List<CmsPage> getObject() {
                        return item.getModelObject().getChilds();
                    }
                };
                CmsPageListView cmsPageListView = new CmsPageListView("page", model);
                item.add(cmsPageListView);
                if (selectedPageId != null && selectedPageId.equals(Long.valueOf(0))) {
                    container.add(new SimpleAttributeModifier("class", "category menu_selected"));
                }
            } else {
                item.add(new EmptyPanel("page").setVisible(false));
            }
        }
    }

    class CmsPageListView extends ListView<CmsPage> {

        CmsPageListView(String id, List<? extends CmsPage> cmsPages) {
            super(id, cmsPages);
        }

        public CmsPageListView(String string, IModel<? extends List<? extends CmsPage>> imodel) {
            super(string, imodel);
        }

        @Override
        protected void populateItem(ListItem<CmsPage> item) {
            CmsLink link = new CmsLink("link", item.getModelObject());
            item.add(link);
            if (selectedPageId != null && selectedPageId.equals(item.getModelObject().getId())) {
                item.add(new SimpleAttributeModifier("class", " page menu_selected"));
            }
            item.add(link);
        }
    }
}
